import { defineStore, createPinia } from "pinia";
import { ConfigState } from "@/stores/interface";
import axios from "axios";

// KeepAliveStore
export const ConfigStore = defineStore({
  id: "ConfigStore",
  state: (): ConfigState => ({
    netBaseHttp: import.meta.env.VITE_API_URL,
    nodeBaseHttp: import.meta.env.VITE_API_NEST_URL,
    vFormBaseHttp: import.meta.env.VITE_FORM_URL,
  }),
  actions: {
    updateBaseHttp(baseHttp: string) {
      this.netBaseHttp = baseHttp;
      this.nodeBaseHttp = baseHttp;
      this.vFormBaseHttp = baseHttp;
    },
    async initConfig() {
      //线上处理
      if (process.env.NODE_ENV == "production" || process.env.NODE_ENV == "test" || process.env.NODE_ENV == "development") {
        await axios
          .get(`config.json?t=${Date.now()}`)
          // .get(`config.json?t=${}`)
          .then(res => {
            this.netBaseHttp = res.data.netBaseHttp || this.netBaseHttp;
            this.nodeBaseHttp = res.data.nodeBaseHttp || this.nodeBaseHttp;
            this.vFormBaseHttp = res.data.vFormBaseHttp || this.vFormBaseHttp;
            this.webSocketUrl = res.data.webSocketUrl || this.webSocketUrl;
          })
          .catch(() => { });
      }
    }
  }
});
